package com.ac.scum.log.task.face.log.counsumer.stages;

import com.ac.common.core.id.SnowflakeIdGenerator;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.bean.ServerFTPLogBank;
import com.ac.scum.log.bean.ServerFTPLogRename;
import com.ac.scum.log.bean.ServerFTPLogTrade;
import com.ac.scum.log.task.face.log.counsumer.LogConsumer;
import com.ac.scum.log.task.mapper.log.ServerFTPLogBankMapper;
import com.ac.scum.log.task.mapper.log.ServerFTPLogRenameMapper;
import com.ac.scum.log.task.mapper.log.ServerFTPLogTradeMapper;
import com.ac.scum.log.task.utils.map.GameMapZoneUtil;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
@AllArgsConstructor
public class EconomyLogConsumer implements LogConsumer {
    private final GameMapZoneUtil gameMapZoneUtil;
    private final SnowflakeIdGenerator snowflakeIdGenerator;
    private final ServerFTPLogBankMapper serverFTPLogBankMapper;
    private final ServerFTPLogRenameMapper serverFTPLogRenameMapper;
    private final ServerFTPLogTradeMapper serverFTPLogTradeMapper;

    @Override
    public void consumerLog(FTPQueueItem ftpQueueItem) {
        writeAllPlayerInfoInCache(ftpQueueItem);
        List<GameLogLine> tradeList = Lists.newArrayList();
        List<GameLogLine> bankList = Lists.newArrayList();
        List<GameLogLine> renameList = Lists.newArrayList();
        List<GameLogLine> discardList = Lists.newArrayList();
        for (GameLogLine ftpLogLine : splitLine(ftpQueueItem)) {
            if (ftpLogLine.getLineContext().contains("[Trade]")) {
                tradeList.add(ftpLogLine);
            } else if (ftpLogLine.getLineContext().contains("[Currency Conversion]") || ftpLogLine.getLineContext().contains("[Bank]")) {
                bankList.add(ftpLogLine);
            } else if (ftpLogLine.getLineContext().contains("changed their name to")) {
                renameList.add(ftpLogLine);
            } else {
                commitDiscardLog(ftpQueueItem, ftpLogLine, "economy no find stage");
            }
        }
        trade(tradeList, ftpQueueItem);
        bank(bankList, ftpQueueItem);
        rename(renameList, ftpQueueItem);
    }

    private void bank(List<GameLogLine> lines, FTPQueueItem ftpQueueItem) {
        try {
            Pattern soldOrPruchasedGoldPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Currency Conversion\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(-?\\d+)\\) (sold|purchased) (-?\\d+) gold for (-?\\d+) credits \\(new account balance is (-?\\d+) gold/(-?\\d+) credits\\) at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)\\.");
            Pattern destroyCardPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Bank\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(-?\\d+)\\) manually destroyed (Starter|Classic|Gold) card belonging to Account Number:(-?\\d+), at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)\\.");
            Pattern purchasedCardPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Bank\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(\\d+)\\) purchased (Classic|Gold) card \\(free renewal: (yes|no)\\), new account balance is (-?\\d+) credits, at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)\\.");
            Pattern cancelledCardPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Bank\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(\\d+)\\) cancelled (Gold|Classic|Starter) card at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)\\.");
            Pattern cancelledCardByPinPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Bank\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(\\d+)\\)'s (Gold|Classic) card was cancelled due to exceeding pin attemps at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)\\.");
            Pattern transferredPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Bank\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(\\d+)\\) transferred (\\d+)\\((\\d+) was removed\\) from Account Number: (\\d+)\\([^\\)]+\\)\\((\\d+)\\) to Account Number: (\\d+)\\([^\\)]+\\)\\((\\d+)\\) at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            Pattern withdrewOrDepositedPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Bank\\] ([^\\(]+)\\(ID:(\\d+)\\)\\(Account Number:(\\d+)\\) (\\w+) (\\d+)\\((\\d+) was (\\w+)\\) (from|to) Account Number: (\\d+)\\([^\\)]+\\)\\((\\w+)\\) at X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            List<ServerFTPLogBank> logBankList = Lists.newArrayList();

            for (GameLogLine line : lines) {
                String lineContext = line.getLineContext();
                Matcher soldOrPruchasedGoldMatcher = soldOrPruchasedGoldPattern.matcher(lineContext);
                Matcher destroyCardMatcher = destroyCardPattern.matcher(lineContext);
                Matcher purchasedCardMatcher = purchasedCardPattern.matcher(lineContext);
                Matcher cancelledCardMatcher = cancelledCardPattern.matcher(lineContext);
                Matcher cancelledCardByPinMatcher = cancelledCardByPinPattern.matcher(lineContext);
                Matcher transferredMatcher = transferredPattern.matcher(lineContext);
                Matcher withdrewOrDepositedMatcher = withdrewOrDepositedPattern.matcher(lineContext);


                ServerFTPLogBank bank = new ServerFTPLogBank();
                bank.setId(snowflakeIdGenerator.nextId());
                bank.setFileId(ftpQueueItem.getFileId());
                bank.setServerId(ftpQueueItem.getServer().getId());
                bank.setLineStart(line.getStartByte());
                bank.setLineEnd(line.getEndByte());
                bank.setCreateTime(LocalDateTime.now());
                //1.购买金条 2.出售金条 3.销毁新手卡 4.销毁黑卡 5.销毁金卡 6.购买黑卡 7.购买金卡 8.注销新手卡 9.注销黑卡 10.注销金卡
                //11.尝试太错误密码而被注销的金卡 12.尝试太错误密码而被注销的黑卡 13.转账 14.取款 15.存款
                if (soldOrPruchasedGoldMatcher.find()) {
                    if (soldOrPruchasedGoldMatcher.group(5).contains("purchased")) {
                        bank.setType(1);
                    } else {
                        bank.setType(2);
                    }
                    bank.setServerTime(parseTimestamp(soldOrPruchasedGoldMatcher.group(1), ftpQueueItem));
                    long steamId = Long.parseLong(soldOrPruchasedGoldMatcher.group(3));
                    Player player = getOrInsertPlayerServer(steamId, bank.getServerId(), soldOrPruchasedGoldMatcher.group(2), soldOrPruchasedGoldMatcher.group(4));
                    bank.setPlayerSteamId(steamId);
                    bank.setPlayerId(player.getId());
                    bank.setPlayerAccountNumber(Long.parseLong(soldOrPruchasedGoldMatcher.group(4)));
                    bank.setGold(Long.parseLong(soldOrPruchasedGoldMatcher.group(6)));
                    bank.setMoney(Long.parseLong(soldOrPruchasedGoldMatcher.group(7)));
                    bank.setAccountGold(Long.parseLong(soldOrPruchasedGoldMatcher.group(8)));
                    bank.setAccountBalance(Long.parseLong(soldOrPruchasedGoldMatcher.group(9)));
                    bank.setLocation(soldOrPruchasedGoldMatcher.group(10) + " " + soldOrPruchasedGoldMatcher.group(11) + " " + soldOrPruchasedGoldMatcher.group(12));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(soldOrPruchasedGoldMatcher.group(11)), Double.parseDouble(soldOrPruchasedGoldMatcher.group(12))));
                } else if (destroyCardMatcher.find()) {
                    String card = destroyCardMatcher.group(5);
                    if (card.contains("Gold")) {
                        bank.setType(5);
                    } else if (card.contains("Classic")) {
                        bank.setType(4);
                    } else {
                        bank.setType(3);
                    }
                    long steamId = Long.parseLong(destroyCardMatcher.group(3));
                    bank.setServerTime(parseTimestamp(destroyCardMatcher.group(1), ftpQueueItem));
                    Player player = getOrInsertPlayerServer(steamId, bank.getServerId(), Strings.EMPTY, destroyCardMatcher.group(4));
                    bank.setPlayerSteamId(steamId);
                    bank.setPlayerId(player.getId());
                    bank.setPlayerAccountNumber(Long.parseLong(destroyCardMatcher.group(4)));
                    bank.setTargetPlayerAccountNumber(Long.parseLong(destroyCardMatcher.group(6)));
                    bank.setLocation(destroyCardMatcher.group(7) + " " + destroyCardMatcher.group(8) + " " + destroyCardMatcher.group(9));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(destroyCardMatcher.group(7)), Double.parseDouble(destroyCardMatcher.group(8))));
                } else if (purchasedCardMatcher.find()) {
                    String card = purchasedCardMatcher.group(5);
                    if (card.contains("Gold")) {
                        bank.setType(7);
                    } else if (card.contains("Classic")) {
                        bank.setType(6);
                    }
                    bank.setServerTime(parseTimestamp(purchasedCardMatcher.group(1), ftpQueueItem));
                    long steamId = Long.parseLong(purchasedCardMatcher.group(3));
                    Player player = getOrInsertPlayerServer(steamId, bank.getServerId(), Strings.EMPTY,purchasedCardMatcher.group(4));
                    bank.setPlayerId(player.getId());
                    bank.setPlayerSteamId(steamId);
                    bank.setPlayerAccountNumber(Long.parseLong(purchasedCardMatcher.group(4)));
                    bank.setFreeRenewalFlag(purchasedCardMatcher.group(6).contains("yes"));
                    bank.setAccountBalance(Long.parseLong(purchasedCardMatcher.group(7)));
                    bank.setLocation(purchasedCardMatcher.group(8) + " " + purchasedCardMatcher.group(9) + " " + purchasedCardMatcher.group(10));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(purchasedCardMatcher.group(8)), Double.parseDouble(purchasedCardMatcher.group(9))));
                } else if (cancelledCardMatcher.find()) {
                    String card = cancelledCardMatcher.group(5);
                    if (card.contains("Gold")) {
                        bank.setType(10);
                    } else if (card.contains("Classic")) {
                        bank.setType(9);
                    } else {
                        bank.setType(8);
                    }
                    bank.setServerTime(parseTimestamp(cancelledCardMatcher.group(1), ftpQueueItem));
                    long steamId = Long.parseLong(cancelledCardMatcher.group(3));
                    Player player = getOrInsertPlayerServer(steamId, bank.getServerId(), Strings.EMPTY,cancelledCardMatcher.group(4));
                    bank.setPlayerSteamId(steamId);
                    bank.setPlayerAccountNumber(Long.parseLong(cancelledCardMatcher.group(4)));
                    bank.setPlayerId(player.getId());
                    bank.setLocation(cancelledCardMatcher.group(6) + " " + cancelledCardMatcher.group(7) + " " + cancelledCardMatcher.group(8));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(cancelledCardMatcher.group(6)), Double.parseDouble(cancelledCardMatcher.group(7))));
                } else if (cancelledCardByPinMatcher.find()) {
                    String card = cancelledCardByPinMatcher.group(5);
                    if (card.contains("Gold")) {
                        bank.setType(11);
                    } else {
                        bank.setType(12);
                    }
                    long steamId = Long.parseLong(cancelledCardByPinMatcher.group(3));
                    Player player = getOrInsertPlayerServer(steamId, bank.getServerId(), Strings.EMPTY,cancelledCardByPinMatcher.group(4));
                    bank.setServerTime(parseTimestamp(cancelledCardByPinMatcher.group(1), ftpQueueItem));
                    bank.setPlayerId(player.getId());
                    bank.setPlayerSteamId(steamId);
                    bank.setPlayerAccountNumber(Long.parseLong(cancelledCardByPinMatcher.group(4)));
                    bank.setLocation(cancelledCardByPinMatcher.group(6) + " " + cancelledCardByPinMatcher.group(7) + " " + cancelledCardByPinMatcher.group(8));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(cancelledCardByPinMatcher.group(6)), Double.parseDouble(cancelledCardByPinMatcher.group(7))));
                } else if (transferredMatcher.find()) {
                    bank.setType(13);
                    bank.setServerTime(parseTimestamp(transferredMatcher.group(1), ftpQueueItem));
                    bank.setPlayerSteamId(Long.parseLong(transferredMatcher.group(3)));
                    Player player = getOrInsertPlayerServer(bank.getPlayerSteamId(), bank.getServerId(), Strings.EMPTY);
                    bank.setPlayerAccountNumber(Long.parseLong(transferredMatcher.group(4)));
                    bank.setPlayerId(player.getId());
                    bank.setMoney(Long.parseLong(transferredMatcher.group(5)));
                    bank.setActualMoney(Long.parseLong(transferredMatcher.group(6)));

                    bank.setFromPlayerAccountNumber(Long.parseLong(transferredMatcher.group(7)));
                    bank.setFromPlayerSteamId(Long.parseLong(transferredMatcher.group(8)));
                    if (bank.getPlayerSteamId().equals(bank.getFromPlayerSteamId())) {
                        bank.setFromPlayerId(bank.getPlayerId());
                    } else {
                        player = getOrInsertPlayerServer(bank.getFromPlayerSteamId(), bank.getServerId(), Strings.EMPTY);
                        bank.setFromPlayerId(player.getId());
                    }
                    bank.setTargetPlayerAccountNumber(Long.parseLong(transferredMatcher.group(9)));
                    bank.setTargetPlayerSteamId(Long.parseLong(transferredMatcher.group(10)));
                    if (bank.getPlayerSteamId().equals(bank.getTargetPlayerSteamId())) {
                        bank.setTargetPlayerId(bank.getPlayerId());
                    } else {
                        player = getOrInsertPlayerServer(bank.getTargetPlayerSteamId(), bank.getServerId(), Strings.EMPTY);
                        bank.setTargetPlayerId(player.getId());
                    }
                    bank.setLocation(transferredMatcher.group(11) + " " + transferredMatcher.group(12) + " " + transferredMatcher.group(13));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(transferredMatcher.group(11)), Double.parseDouble(transferredMatcher.group(12))));
                } else if (withdrewOrDepositedMatcher.find()) {
                    if (withdrewOrDepositedMatcher.group(5).contains("deposited")) {
                        bank.setType(15);
                    } else {
                        bank.setType(14);
                    }
                    bank.setServerTime(parseTimestamp(withdrewOrDepositedMatcher.group(1), ftpQueueItem));
                    bank.setPlayerSteamId(Long.parseLong(withdrewOrDepositedMatcher.group(3)));
                    bank.setPlayerAccountNumber(Long.parseLong(withdrewOrDepositedMatcher.group(4)));
                    Player player = getOrInsertPlayerServer(bank.getPlayerSteamId(), bank.getServerId(), Strings.EMPTY);
                    bank.setPlayerId(player.getId());
                    bank.setMoney(Long.parseLong(withdrewOrDepositedMatcher.group(6)));
                    bank.setActualMoney(Long.parseLong(withdrewOrDepositedMatcher.group(7)));
                    bank.setTargetPlayerAccountNumber(Long.parseLong(withdrewOrDepositedMatcher.group(10)));
                    bank.setTargetPlayerSteamId(Long.parseLong(withdrewOrDepositedMatcher.group(11)));
                    if (bank.getPlayerSteamId().equals(bank.getTargetPlayerSteamId())) {
                        bank.setTargetPlayerId(bank.getPlayerId());
                    } else {
                        player = getOrInsertPlayerServer(bank.getTargetPlayerSteamId(), bank.getServerId(), Strings.EMPTY);
                        bank.setTargetPlayerId(player.getId());
                    }
                    bank.setLocation(withdrewOrDepositedMatcher.group(12) + " " + withdrewOrDepositedMatcher.group(13) + " " + withdrewOrDepositedMatcher.group(14));
                    bank.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(withdrewOrDepositedMatcher.group(12)), Double.parseDouble(withdrewOrDepositedMatcher.group(13))));
                } else {
                    commitDiscardLog(ftpQueueItem, line, "bank no match");
                }
                logBankList.add(bank);
            }
            logBankList.forEach(serverFTPLogBankMapper::insert);
        } catch (Exception e) {
            log.error("银行日志任务出现异常", e);
        }
    }

    private void rename(List<GameLogLine> lines, FTPQueueItem ftpQueueItem) {
        Pattern renamePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): Player ([^\\(]+) \\((\\d+), (\\d+)\\) changed their name to ([^\\(]+)\\.");
        for (GameLogLine line : lines) {
            String lineContext = line.getLineContext();
            Matcher renameMatcher = renamePattern.matcher(lineContext);

            ServerFTPLogRename logRename = new ServerFTPLogRename();
            logRename.setId(snowflakeIdGenerator.nextId());
            logRename.setId(snowflakeIdGenerator.nextId());
            logRename.setFileId(ftpQueueItem.getFileId());
            logRename.setServerId(ftpQueueItem.getServer().getId());
            logRename.setLineStart(line.getStartByte());
            logRename.setLineEnd(line.getEndByte());
            logRename.setCreateTime(LocalDateTime.now());
            if (renameMatcher.find()) {
                logRename.setServerTime(parseTimestamp(renameMatcher.group(1), ftpQueueItem));
                logRename.setPlayerSteamId(Long.parseLong(renameMatcher.group(4)));
                Player player = getOrInsertPlayerServer(logRename.getPlayerSteamId(), logRename.getServerId(), Strings.EMPTY);
                logRename.setPlayerId(player.getId());
                logRename.setPlayerName(player.getName());
                logRename.setChangeName(renameMatcher.group(5));
            } else {
                commitDiscardLog(ftpQueueItem, line, "rename no match");
            }
            serverFTPLogRenameMapper.insert(logRename);
        }
    }

    private void trade(List<GameLogLine> lines, FTPQueueItem ftpQueueItem) {
        try {
            Pattern soldTradeablePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Trade\\] Tradeable \\((.+?) \\(x(\\d+)\\)\\) sold by ([^\\(]+)\\((\\d+)\\) for (\\d+) .*? to trader (.*?), old amount in store is (-?\\d+), new amount is (-?\\d+), and effective users online: (\\d+)");
            Pattern beforeSoldTradeablePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Trade\\] Before selling tradeables to trader (.+?), player ([^\\(]+)\\((\\d+)\\) had (-?\\d+) cash, (-?\\d+) account balance and (-?\\d+) gold and trader had (\\d+) funds.");
            Pattern afterSoldTradeablePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Trade\\] After tradeable sale to trader (.+?), player ([^\\(]+)\\((\\d+)\\) has (-?\\d+) cash, (-?\\d+) account balance and (-?\\d+) gold and trader has (\\d+) funds.");

            Pattern purchasedTradeablePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Trade\\] Tradeable \\((.+?) \\(x(\\d+)\\)\\) purchased by ([^\\(]+)\\((\\d+)\\) for (\\d+) .*? from trader (.*?), old amount in store was (-?\\d+), new amount is (-?\\d+), and effective users online: (\\d+)");
            Pattern beforePurchasedTradeablePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Trade\\] Before purchasing tradeales from trader (.+?), player ([^\\(]+)\\((\\d+)\\) had (-?\\d+) cash, (-?\\d+) account balance and (-?\\d+) gold and trader had (\\d+) funds.");
            Pattern afterPurchasedTradeablePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[Trade\\] After tradeable purchase from trader (.+?), player ([^\\(]+)\\((\\d+)\\) has (-?\\d+) cash, (-?\\d+) bank account balance and (-?\\d+) gold and trader has (\\d+) funds.");

            for (GameLogLine line : lines) {
                String lineContext = line.getLineContext();
                Matcher soldTradeableMatcher = soldTradeablePattern.matcher(lineContext);
                Matcher beforeSoldTradeableMatcher = beforeSoldTradeablePattern.matcher(lineContext);
                Matcher afterSoldTradeableMatcher = afterSoldTradeablePattern.matcher(lineContext);

                Matcher purchasedTradeableMatcher = purchasedTradeablePattern.matcher(lineContext);
                Matcher beforePurchasedTradeableMatcher = beforePurchasedTradeablePattern.matcher(lineContext);
                Matcher afterPurchasedTradeableMatcher = afterPurchasedTradeablePattern.matcher(lineContext);

                ServerFTPLogTrade trade = new ServerFTPLogTrade();
                trade.setId(snowflakeIdGenerator.nextId());
                trade.setFileId(ftpQueueItem.getFileId());
                trade.setServerId(ftpQueueItem.getServer().getId());
                trade.setLineStart(line.getStartByte());
                trade.setLineEnd(line.getEndByte());
                trade.setCreateTime(LocalDateTime.now());
                //1.玩家出售详情 2出售之前的详情 3出售之后的详情 4购买详情 5购买之前的详情 6购买之后的详情
                if (soldTradeableMatcher.find()) {
                    trade.setType(1);
                    trade.setServerTime(parseTimestamp(soldTradeableMatcher.group(1), ftpQueueItem));
                    trade.setItem(getSpawnItemCodeCnName(soldTradeableMatcher.group(2)));
                    trade.setQuantity(Integer.parseInt(soldTradeableMatcher.group(3)));
                    assembleTradePlayer(trade, soldTradeableMatcher.group(5));
                    trade.setPrice(soldTradeableMatcher.group(6));
                    String[] splitTrader = soldTradeableMatcher.group(7).split("_");
                    trade.setZone(splitTrader[0] + splitTrader[1]);
                    trade.setTrader(splitTrader[2]);
                    trade.setOldAmount(soldTradeableMatcher.group(8));
                    trade.setNewAmount(soldTradeableMatcher.group(9));
                } else if (beforeSoldTradeableMatcher.find()) {
                    trade.setType(2);
                    trade.setServerTime(parseTimestamp(beforeSoldTradeableMatcher.group(1), ftpQueueItem));
                    String[] splitTrader = beforeSoldTradeableMatcher.group(2).split("_");
                    trade.setZone(splitTrader[0] + splitTrader[1]);
                    trade.setTrader(splitTrader[2]);
                    assembleTradePlayer(trade, beforeSoldTradeableMatcher.group(4));
                    trade.setCash(Long.parseLong(beforeSoldTradeableMatcher.group(5)));
                    trade.setPlayerAccountBalance(Long.parseLong(beforeSoldTradeableMatcher.group(6)));
                    trade.setPlayerGold(Long.parseLong(beforeSoldTradeableMatcher.group(7)));
                    trade.setOldAmount(beforeSoldTradeableMatcher.group(8));
                } else if (afterSoldTradeableMatcher.find()) {
                    trade.setType(3);
                    trade.setServerTime(parseTimestamp(afterSoldTradeableMatcher.group(1), ftpQueueItem));
                    String[] splitTrader = afterSoldTradeableMatcher.group(2).split("_");
                    trade.setZone(splitTrader[0] + splitTrader[1]);
                    trade.setTrader(splitTrader[2]);
                    assembleTradePlayer(trade, afterSoldTradeableMatcher.group(4));
                    trade.setCash(Long.parseLong(afterSoldTradeableMatcher.group(5)));
                    trade.setPlayerAccountBalance(Long.parseLong(afterSoldTradeableMatcher.group(6)));
                    trade.setPlayerGold(Long.parseLong(afterSoldTradeableMatcher.group(7)));
                    trade.setNewAmount(afterSoldTradeableMatcher.group(8));
                } else if (purchasedTradeableMatcher.find()) {
                    trade.setType(4);
                    trade.setServerTime(parseTimestamp(purchasedTradeableMatcher.group(1), ftpQueueItem));
                    String[] splitTrader = purchasedTradeableMatcher.group(7).split("_");
                    trade.setZone(splitTrader[0] + splitTrader[1]);
                    trade.setTrader(splitTrader[2]);
                    assembleTradePlayer(trade, purchasedTradeableMatcher.group(5));
                    trade.setItem(getSpawnItemCodeCnName(purchasedTradeableMatcher.group(2)));
                    trade.setQuantity(Integer.parseInt(purchasedTradeableMatcher.group(3)));
                    trade.setPrice(purchasedTradeableMatcher.group(6));
                    trade.setOldAmount(purchasedTradeableMatcher.group(8));
                    trade.setNewAmount(purchasedTradeableMatcher.group(9));
                } else if (beforePurchasedTradeableMatcher.find()) {
                    trade.setType(5);
                    trade.setServerTime(parseTimestamp(beforePurchasedTradeableMatcher.group(1), ftpQueueItem));
                    String[] splitTrader = beforePurchasedTradeableMatcher.group(2).split("_");
                    trade.setZone(splitTrader[0] + splitTrader[1]);
                    trade.setTrader(splitTrader[2]);
                    assembleTradePlayer(trade, beforePurchasedTradeableMatcher.group(4));
                    trade.setCash(Long.parseLong(beforePurchasedTradeableMatcher.group(5)));
                    trade.setPlayerAccountBalance(Long.parseLong(beforePurchasedTradeableMatcher.group(6)));
                    trade.setPlayerGold(Long.parseLong(beforePurchasedTradeableMatcher.group(7)));
                    trade.setOldAmount(beforePurchasedTradeableMatcher.group(8));
                } else if (afterPurchasedTradeableMatcher.find()) {
                    trade.setType(6);
                    trade.setServerTime(parseTimestamp(afterPurchasedTradeableMatcher.group(1), ftpQueueItem));
                    String[] splitTrader = afterPurchasedTradeableMatcher.group(2).split("_");
                    trade.setZone(splitTrader[0] + splitTrader[1]);
                    trade.setTrader(splitTrader[2]);
                    assembleTradePlayer(trade, afterPurchasedTradeableMatcher.group(4));
                    trade.setCash(Long.parseLong(afterPurchasedTradeableMatcher.group(5)));
                    trade.setPlayerAccountBalance(Long.parseLong(afterPurchasedTradeableMatcher.group(6)));
                    trade.setPlayerGold(Long.parseLong(afterPurchasedTradeableMatcher.group(7)));
                    trade.setNewAmount(afterPurchasedTradeableMatcher.group(8));
                } else {
                    commitDiscardLog(ftpQueueItem, line, "trade no match");
                    continue;
                }
                serverFTPLogTradeMapper.insert(trade);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private void assembleTradePlayer(ServerFTPLogTrade trade, String steamId) {
        long steamIdL = Long.parseLong(steamId);
        Player player = getOrInsertPlayerServer(steamIdL, trade.getServerId(), Strings.EMPTY);
        trade.setPlayerSteamId(steamIdL);
        trade.setPlayerId(player.getId());

    }

}
